/*
 * Copyright (c) GoKeep by gokeep.org. 2022-2023. All rights reserved
 */
package org.gokeep.common.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

/**
 * @author xuning
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DateUtil {
    public static final String FORMAT = "YYYYMM";
    public static final long DAY_MILLIS_SECOND = 1000 * 3600 * 24L;
    public static final DateTimeFormatter COLLECTION_SUFFIX_FORMAT = DateTimeFormatter.ofPattern(FORMAT, Locale.CHINA);

    /**
     * 根据当前时间获取集合后缀
     *
     * @return
     */
    public static String getMongoCollectionSuffixWithNow() {
        return LocalDateTime.now().format(COLLECTION_SUFFIX_FORMAT);
    }


    /**
     * 获取几天之后时间点
     * @param dayNum
     * @return
     */
    public static Date getAfterDay(int dayNum) {
        return new Date(System.currentTimeMillis() + DAY_MILLIS_SECOND * dayNum);
    }


    /**
     * 获取一周之后的时间点
     * @return
     */
    public static Date getAfterWeek() {
        return getAfterDay(7);
    }

    /**
     * 获取当前系统时间戳
     * @return
     */
    public static long now() {
        return System.currentTimeMillis();
    }

    /**
     * 时间戳转换为字符串
     * @param timestamp
     * @return
     */
    public static String timeToStr(long timestamp) {
        final SimpleDateFormat defaultDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return defaultDateFormat.format(timestamp);
    }
}
